<?php

/**
 * 后台控制器父类
 */
class AdminController extends Yaf_Controller_Abstract
{

    public function init()
    {
        //初始化后台视图引擎
        $this->setViewpath(APPLICATION_PATH . '/application/modules/Admin/views');
        $this->initView();
// 		debug($_SESSION);
        //判断是否登录
        if (!Yaf_Session::getInstance()->get("admin_username")) {
            $this->redirect('/Admin/login/in');
            exit();
        }
        
        $this->_view->assign("username", Yaf_Session::getInstance()->get("admin_username"));
        $this->_view->assign("rolename", '管理员');
        $this->_view->assign("logintime", date('Y-m-d H:i:s'));
        $this->_view->assign("title", '');
        $this->_view->assign("activetitle", '');
        $this->_view->assign("parentmenu", '');
        $this->_view->assign("action", '');
        
        //获取菜单，菜单默认缓存在session中，修改需要重新登陆后刷新
        if (!Yaf_Session::getInstance()->get("admin_menu")){
            if ($this->isadmin()){
                $menu = $this->getmenu(1);
            }else{
                $menu = $this->getmenu(0);
            }
            Yaf_Session::getInstance()->set("admin_menu", $menu);
            $this->_view->assign('menus', $menu);
        }else{
            $this->_view->assign('menus', Yaf_Session::getInstance()->get("admin_menu"));
        }
       
        //检查访问权限
        if (!$this->checkaccess()){
            $this->_view->assign("msg", '您无权进行这个操作！');
            $this->_view->display('public/error');
        }else{
        	//记录日志
        	$config = Yaf_Registry::get('config');
        	if($config->toArray()['log']){
        		$log = new LogModel();
        		$pow = new PowerModel();
//         		if($this->getRequest()->getMethod() == 'POST'){        			
		        	$controller = strtolower($this->getRequest()->getControllerName());
		        	$aciton = strtolower($this->getRequest()->getActionName());
		        	$uri = $controller.'/'.$aciton;
		        	if($this->getRequest()->getParams()){
		        		foreach ($this->getRequest()->getParams() as $k=>$v){
		        			$uri.='/'.$k.'/'.$v;
		        		}
		        	}
		        	$title = $pow->ufind($controller, $aciton);
		        	$title = (array)$title;

		        	if(!empty($title)){
		        		$info['operate'] = $title['title'];
			        	$info['uri'] = $uri;
			        	$info['operator'] = Yaf_Session::getInstance()->get('admin_username');
			        	$info['ip'] = getremoteip();
			        	$log->insert($info);      		
		        	}
//         		}
        	}        	        	
        }
    }
    
    public function isadmin()
    {
        //判断是否登录
        if (!Yaf_Session::getInstance()->get("admin_username")) {
            $this->redirect('/Admin/login/in');
        }
        
        if (Yaf_Session::getInstance()->get("admin_username") == 'admin' &&
            Yaf_Session::getInstance()->get("admin_uid") == 1) {
            return true;
        }else{
            return false;
        }
    }
    
    private function checkaccess()
    {
        if (Yaf_Session::getInstance()->get("admin_uid") == 1){
            return true;
        }

        $controller = strtolower($this->getRequest()->getControllerName());
        $aciton = strtolower($this->getRequest()->getActionName());
        if (($controller == 'index' && $aciton == "index") ||
            ($controller == 'user' && $aciton == "changepwd")){
            return true;
        }
        
        $role = new RoleModel();
        if ($role->checkaccess(Yaf_Session::getInstance()->get("admin_uid"), $controller, $aciton)){
            return true;
        }else{
            return false;
        }
    }
    
    private function getmenu($isview = 1){
        $usermenu = $this->getusermenu();
        $menu = array(
            //用户管理菜单
            array(
                'view' => 1,
                'title' => '用户管理',
                'url' => '',
                'sub' => array(
                    array(
                        'view' => $isview,
                        'title' => '角色管理',
                        'url' => '/admin/role/list',
                    ),
                    array(
                        'view' => $isview,
                        'title' => '管理员管理',
                        'url' => '/admin/user/list',                    	
                    ),
                    array(
                        'view' => 1,
                        'title' => '修改密码',
                        'url' => '/admin/user/changepwd',
                    ),
                )
            ),
            //系统管理菜单
            array(
                'view' => $isview,
                'title' => '系统管理',
                'url' => '',
                'sub' => array(
                    array(
                        'view' => $isview,
                        'title' => '权限管理',
                        'url' => '/admin/power/list',                    		
                    ),
                		
                	array(
                		'view' => $isview,
                		'title' => '缓存清理',
                		'url' => '/admin/sys/clean',
                	),
                		
                	array(
                		'view' => $isview,
                		'title' => '数据库操作',
                		'url' => '/admin/sys/db',
                	),
                		
                    array(
                        'view' => $isview,
                        'title' => '系统日志',
                        'url' => '/admin/sys/log',
                    ),
                )
            ),
        );
        return array_merge($usermenu,$menu);
    }
    
    private function getusermenu()
    {
        $roleid = Yaf_Session::getInstance()->get("admin_roleid");
        $role = new RoleModel();
        $accessarray = $role->getaccess_toplevel($roleid);
        
        $newarray = array();
        foreach($accessarray as $access){
            $subaccess = $role->getaccess_sub($roleid, $access['powerid']);
            if ($subaccess){
                $access['sub'] = $subaccess;
            }
            array_push($newarray, $access);
        }
        return $newarray;
    }
    
    private function getmap($url,$array){    	
    	foreach ($array as $v){
    		if($url == $v['url']){
    			return $v;
    			break;
    		}else{
    			if(isset($v['sub'])){
    				$result = $this->getmap($url,$v['sub']);
    				if ($result)
    					return $result;
    			}
    		}
    	}
    }
}